﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Gallery.Repositories;
using MvcEngine.Gallery.Models;
using MvcEngine.Core.Pagination;
using MvcEngine.Core.Extensions;
using MvcEngine.LinqToSqlProvider;

namespace MvcEngine.Gallery.LinqToSqlProvider
{
    public class GalleryImageCategoryRepository : Repository, IGalleryImageCategoryRepository
    {
        public void DeleteAll(Guid imageId)
        {
            var categories = GetAll(imageId);
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.GalleryImageCategories().DeleteAllOnSubmit(categories);
                contextManager.Commit();
            }
        }

        public void DeleteAll(IEnumerable<GalleryImageCategory> imageCategories)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.GalleryImageCategories().DeleteAllOnSubmit(imageCategories);
                contextManager.Commit();
            }
        }

        public IList<GalleryImageCategory> GetAll(Guid imageId)
        {
            using (var contextManager = CreateContextManager(true))
            {
                return contextManager.Context.GalleryImageCategories().Where(p => p.ImageId == imageId).ToList();
            }
        }

        public void InsertAll(IEnumerable<GalleryImageCategory> imageCategories)
        {
            using (var contextManager = CreateContextManager(false))
            {
                contextManager.Context.GalleryImageCategories().InsertAllOnSubmit(imageCategories);
                contextManager.Commit();
            }
        }
    }
}
